﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Messaging;
using Bymed.SpineScan.WPF.Shared;
using Bymed.SpineScan.WPF.ViewModel;
using Bymed.SpineScan.WPF.ViewModel.TestItemViewModel;

namespace Bymed.SpineScan.WPF
{
    /// <summary>
    /// Interaction logic for StartTestPage.xaml
    /// </summary>
    public partial class StartTestPage : PageBase
    {
        public StartTestPage()
        {
            InitializeComponent();
            InitializePageUnloadStoryboard();
        }

        protected override void RegisterMessages()
        {
            base.RegisterMessages();
            RegisterMoveToNextTestItemMessage();
            RegisterGoToNextPageMessage();
        }

        /// <summary>
        /// Register the MoveToNextTestItemMessage from StartTestViewModel. Then ListBoxTestItems will execute ScrollIntoView method.
        /// </summary>
        private void RegisterMoveToNextTestItemMessage()
        {
            Messenger.Default.Register<SingleTestItemViewModel>(this, StartTestViewModel.MoveToNextTestItemMessageToken, p =>
                {
                    ListBoxTestItems.Dispatcher.Invoke(() =>
                        {
                            ListBoxTestItems.ScrollIntoView(ListBoxTestItems.SelectedItem);
                        });
                });
        }

        /// <summary>
        /// Register the GoToNextPageMessage from StartTestViewModel.
        /// </summary>
        private void RegisterGoToNextPageMessage()
        {
            Messenger.Default.Register<StartTestViewModel>(this, StartTestViewModel.GoToNextPageMessageToken, p =>
            {
                try
                {
                    var testSummaryReportPage = new TestSummaryPage();
                    var testSummaryReportViewModel = testSummaryReportPage.DataContext as TestSummaryViewModel;
                    ViewModelHelper.CopyTestItemAndAllRelatedInfoReference(p, testSummaryReportViewModel);

                    GoToPage(testSummaryReportPage);
                }
                catch (Exception ex)
                {
                    MyViewModelBase.RequestDisplayErrorMessage(ex.Message);
                }
            });
        }

        private void ButtonBack_Click(object sender, RoutedEventArgs e)
        {
            GoToPage(new SelectTestItemPage(), false);
        }
    }
}
